using System.Reflection;
using System.Text;

namespace OpenPms.Areas.M.Pages.Setup.Rate;

public class EditPriceModel : PageModel
{
    private readonly Data.AppDbContext db;
    public EditPriceModel(Data.AppDbContext db)
    {
        this.db = db;
    }
    public ERateType RateType { get => TempData.Peek<ERateType>(ETempDataKey.RateType); }
    public string? RateName { get => TempData.Peek<string>(ETempDataKey.RateName); }
    public Guid RateId { get => TempData.Peek<Guid>(ETempDataKey.RateId); }
    public bool IsDelayPrice { get => TempData.Peek<bool>(ETempDataKey.IsDelayPrice); }
    public List<TPrice> Items { get; set; } = default!;
    [BindProperty]
    public TPrice Item { get; set; } = default!;

    public async Task<IActionResult> OnGetAsync()
    {

        Items = await db.Prices.Where(ii => ii.RateId == RateId).Select(ii => new TPrice { Date = ii.Date }).Distinct().OrderBy(ii => ii.Date).ToListAsync();

        if (Items.Count == 0)
            return RedirectToPage("./EditPriceDetail");
        TPrice.SetDay(Items);
        return Page();
    }


}
